//
//  ViewController.m
//  LabelLineHeight
//
//  Created by Dai-zhan on 2019/10/15.
//  Copyright © 2019 Dai-zhan. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    //MVVM模式试图获得MVC提供的功能性开发分离的两个优点，同时利用数据绑定的优势和通过绑定数据的框架尽可能接近纯应用程序模型。它使用绑定器、视图模型和任何业务层的数据检查功能来验证传入的数据。结果是模型和框架驱动尽可能多的操作，消除或最小化直接操纵视图的应用程序逻辑。
    
    NSString *lblString = @"MVVM模式试图获得MVC提供的功能性开发分离的两个优点，同时利用数据绑定的优势和通过绑定数据的框架尽可能接近纯应用程序模型。它使用绑定器、视图模型和任何业务层的数据检查功能来验证传入的数据。结果是模型和框架驱动尽可能多的操作，消除或最小化直接操纵视图的应用程序逻辑。";
    
    UILabel *lbl = [[UILabel alloc] init];
    lbl.text = lblString;
//    lbl.backgroundColor = [UIColor cyanColor];
    lbl.textColor = [UIColor magentaColor];
    lbl.font = [UIFont systemFontOfSize:20];
    lbl.numberOfLines = 0;
    [self.view addSubview:lbl];
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    lbl.frame = CGRectMake(10, 100, width - 20, height - 200);
    
    NSLog(@"%lf--%lf", lbl.font.lineHeight, lbl.font.pointSize);
    
    CGFloat off = lbl.font.lineHeight - lbl.font.pointSize;
    lbl.attributedText = [self textAttributeWithText:lblString Off:off lineSpacing:10];
}

- (NSAttributedString *)textAttributeWithText:(NSString *)text Off:(CGFloat)off lineSpacing:(CGFloat)lineSpacing {
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineSpacing = lineSpacing - off;
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    [attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    [attributes setObject:[UIColor cyanColor] forKey:NSBackgroundColorAttributeName];
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}


@end
